package com.ruoyi.point.service.impl;

import java.time.Duration;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.data.redis.core.RedisTemplate;
import com.ruoyi.point.mapper.UsersMapper;
import com.ruoyi.point.domain.Users;
import com.ruoyi.point.service.IUsersService;

/**
 * 用户Service业务层处理
 * 
 * @author hy
 * @date 2025-08-12
 */
@Service("pointUsersServiceImpl")
public class UsersServiceImpl implements IUsersService 
{
    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 缓存常量
    private static final String USERS_DETAIL_KEY = "users:detail:";
    private static final String USERS_PHONE_KEY = "users:phone:";
    private static final Duration USERS_DETAIL_EXPIRE = Duration.ofMinutes(30);
    private static final Duration USERS_PHONE_EXPIRE = Duration.ofMinutes(20);

    /**
     * 查询用户
     * 
     * @param id 用户主键
     * @return 用户
     */
    @Override
    public Users selectUsersById(Integer id)
    {
        return usersMapper.selectUsersById(id);
    }

    /**
     * 查询用户列表
     * 
     * @param users 用户
     * @return 用户
     */
    @Override
    public List<Users> selectUsersList(Users users)
    {
        return usersMapper.selectUsersList(users);
    }

    /**
     * 新增用户
     * 
     * @param users 用户
     * @return 结果
     */
    @Override
    public int insertUsers(Users users)
    {
        int result = usersMapper.insertUsers(users);
        if (result > 0) {
            // 清除相关缓存
            clearUsersCache();
        }
        return result;
    }

    /**
     * 修改用户
     * 
     * @param users 用户
     * @return 结果
     */
    @Override
    public int updateUsers(Users users)
    {
        int result = usersMapper.updateUsers(users);
        if (result > 0) {
            // 清除相关缓存
            clearUsersCache();
            clearUsersDetailCache(users.getId());
            if (users.getPhone() != null) {
                clearUsersPhoneCache(users.getPhone());
            }
        }
        return result;
    }

    /**
     * 批量删除用户
     * 
     * @param ids 需要删除的用户主键集合
     * @return 结果
     */
    @Override
    public int deleteUsersByIds(Integer[] ids)
    {
        int result = usersMapper.deleteUsersByIds(ids);
        if (result > 0) {
            // 清除相关缓存
            clearUsersCache();
            for (Integer id : ids) {
                clearUsersDetailCache(id);
            }
        }
        return result;
    }

    /**
     * 删除用户信息
     * 
     * @param id 用户主键
     * @return 结果
     */
    @Override
    public int deleteUsersById(Integer id)
    {
        int result = usersMapper.deleteUsersById(id);
        if (result > 0) {
            clearUsersDetailCache(id);
            clearUsersCache();
        }
        return result;
    }

    /**
     * 根据ID查询用户详情（多表联查，包含积分统计）
     * 
     * @param id 用户ID
     * @return 用户详情
     */
    @Override
    public Users selectUsersDetailById(Integer id)
    {
        String cacheKey = USERS_DETAIL_KEY + id;
        
        // 尝试从缓存获取
        Users cached = (Users) redisTemplate.opsForValue().get(cacheKey);
        if (cached != null) {
            return cached;
        }
        
        // 从数据库查询
        Users result = usersMapper.selectUsersDetailById(id);
        if (result != null) {
            // 存入缓存
            redisTemplate.opsForValue().set(cacheKey, result, USERS_DETAIL_EXPIRE);
        }
        
        return result;
    }

    /**
     * 根据手机号查询用户信息
     * 
     * @param phone 手机号
     * @return 用户信息
     */
    @Override
    public Users selectUsersByPhone(String phone)
    {
        String cacheKey = USERS_PHONE_KEY + phone;
        
        // 尝试从缓存获取
        Users cached = (Users) redisTemplate.opsForValue().get(cacheKey);
        if (cached != null) {
            return cached;
        }
        
        // 从数据库查询
        Users result = usersMapper.selectUsersByPhone(phone);
        if (result != null) {
            // 存入缓存
            redisTemplate.opsForValue().set(cacheKey, result, USERS_PHONE_EXPIRE);
        }
        
        return result;
    }

    /**
     * 清除用户详情缓存
     */
    private void clearUsersDetailCache(Integer id) {
        redisTemplate.delete(USERS_DETAIL_KEY + id);
    }

    /**
     * 清除用户手机号缓存
     */
    private void clearUsersPhoneCache(String phone) {
        redisTemplate.delete(USERS_PHONE_KEY + phone);
    }

    /**
     * 清除所有用户缓存
     */
    private void clearUsersCache() {
        // 清除所有相关缓存
        redisTemplate.delete(USERS_DETAIL_KEY + "*");
        redisTemplate.delete(USERS_PHONE_KEY + "*");
    }
}
